<?php
/**
 * Created by PhpStorm.
 * User: wjj
 * Date: 2016/11/1
 */

namespace app\logic;


use app\models\CustomerService;
use app\models\Express;
use app\models\GoodsSku;
use app\models\Channel;
use app\models\Order;
use app\models\OrderGoods;
use app\models\Package;
use app\models\Refund;
use Pimple\Tests\Fixtures\Service;
use yii;


class OrderLogic
{
    public static $input_type_list = ['下拉框','输入框','单选项','多选项'];

    public function getOrder($query = [], $page = 1, $limit = 10)
    {
        $order = Order::find()
            ->select('o.*,g.goods_name,g.real_price,c.category_name,u.mobile,m.mall_name')
            ->from('{{%order}} o')
            ->leftJoin('{{%goods}} g', 'g.goods_id=o.goods_id')
            ->leftJoin('{{%category}} c', 'c.category_id=g.category_id')
            ->leftJoin('{{%user}} u', 'u.user_id=o.user_id')
            ->leftJoin('{{%mall}} m', 'm.mall_id=o.mall_id')
            ->where("1=1");
        if(isset($query['order_status']) && $query['order_status']!=-1){
            if($query['order_status']==4){
                $order->andwhere(['o.refund_status'=>$query['order_status']]);
            }else{
                $order->andwhere(['o.order_status'=>$query['order_status']]);
            }
        }/*else{
            $order->where("order_status=1");
        }*/

        if(isset($query['order_no']) && $query['order_no']!=''){
            $order->andWhere(['o.order_no'=>$query['order_no']]);
        }
        if(isset($query['mobile']) && $query['mobile']!=''){
            $order->andWhere(['u.mobile'=>$query['mobile']]);
        }

        if(isset($query['shipping_status']) && $query['shipping_status']!=-1){
            $order->andWhere(['o.shipping_status'=>$query['shipping_status']]);
        }

        if(isset($query['goods_id']) && $query['goods_id']!=0){
            $order->andWhere(['o.goods_id'=>$query['goods_id']]);
        }
        if(isset($query['goods_name']) && $query['goods_name']!=''){
            $order->andWhere(['g.goods_name'=>$query['goods_name']]);
        }
        if(isset($query['category_id']) && $query['category_id']!=0){
            $order->andWhere(['c.category_id'=>$query['category_id']]);
        }
        if(isset($query['mall_id']) && $query['mall_id']!=0){
            $order->andWhere(['o.mall_id'=>$query['mall_id']]);
        }

        if(isset($query['order_start_time']) && $query['order_start_time']!=''){
            $order->andWhere(['>=','o.order_time',date("Y-m-d 00:00:00", strtotime($query['order_start_time']))]);
        }
        if(isset($query['order_end_time']) && $query['order_end_time']!=''){
            $order->andWhere(['<=','o.order_time',date("Y-m-d 23:59:59", strtotime($query['order_end_time']))]);
        }

        if(isset($query['pay_start_time']) && $query['pay_start_time']!=''){
            $order->andWhere(['>=','o.pay_time',date("Y-m-d 00:00:00", strtotime($query['pay_start_time']))]);
        }
        if(isset($query['pay_end_time']) && $query['pay_end_time']!=''){
            $order->andWhere(['<=','o.pay_time',date("Y-m-d 23:59:59", strtotime($query['pay_end_time']))]);
        }

            $order->orderBy('order_id desc');
        if($limit)
            $order->offset(($page - 1) * $limit)->limit($limit);
        $lists = $order->asArray()->all();
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $order->count(),
                'page' => $page,
                'limit' => $limit,
                'url' => 'order/index',
                'query' => $query
            ]
        ];
        return $return;
    }


    public function getShippingOrder($query = [], $page = 1, $limit = 10)
    {
        $order = Order::find()
            ->select('o.*,g.goods_name,g.real_price,c.category_name,m.mall_name')
            ->from('{{%order}} o')
            ->leftJoin('{{%goods}} g', 'g.goods_id=o.goods_id')
            ->leftJoin('{{%category}} c', 'c.category_id=g.category_id')
            ->leftJoin('{{%mall}} m', 'm.mall_id=o.mall_id')
            ->where("order_status=1 AND refund_status=0");


        if(isset($query['order_no']) && $query['order_no']!=''){
            $order->andWhere(['o.order_no'=>$query['order_no']]);
        }

        if(isset($query['goods_id']) && $query['goods_id']!=0){
            $order->andWhere(['o.goods_id'=>$query['goods_id']]);
        }
        if(isset($query['goods_name']) && $query['goods_name']!=''){
            $order->andWhere(['g.goods_name'=>$query['goods_name']]);
        }
        if(isset($query['category_id']) && $query['category_id']!=0){
            $order->andWhere(['c.category_id'=>$query['category_id']]);
        }

        if(isset($query['order_start_time']) && $query['order_start_time']!=''){
            $order->andWhere(['>=','o.order_time',date("Y-m-d 00:00:00", strtotime($query['order_start_time']))]);
        }
        if(isset($query['order_end_time']) && $query['order_end_time']!=''){
            $order->andWhere(['<=','o.order_time',date("Y-m-d 23:59:59", strtotime($query['order_end_time']))]);
        }

        if(isset($query['pay_start_time']) && $query['pay_start_time']!=''){
            $order->andWhere(['>=','o.pay_time',date("Y-m-d 00:00:00", strtotime($query['pay_start_time']))]);
        }
        if(isset($query['pay_end_time']) && $query['pay_end_time']!=''){
            $order->andWhere(['<=','o.pay_time',date("Y-m-d 23:59:59", strtotime($query['pay_end_time']))]);
        }

        $order->orderBy('order_id desc');
        if($limit)
            $order->offset(($page - 1) * $limit)->limit($limit);
        $lists = $order->asArray()->all();
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $order->count(),
                'page' => $page,
                'limit' => $limit,
                'url' => 'order/shipping',
                'query' => $query
            ]
        ];
        return $return;
    }

    public function getRefundOrder($query = [], $page = 1, $limit = 10)
    {
        $order = Refund::find()
            ->select('r.*,o.order_no,o.pay_type,o.pay_no,o.order_amount,u.mobile,g.goods_name,g.real_price,c.category_name,m.mall_name,o.order_time,o.pay_time')
            ->from('{{%refund}} r')
            ->leftJoin('{{%order}} o', 'o.order_id=r.order_id')
            ->leftJoin('{{%goods}} g', 'g.goods_id=o.goods_id')
            ->leftJoin('{{%category}} c', 'c.category_id=g.category_id')
            ->leftJoin('{{%user}} u', 'u.user_id=o.user_id')
            ->leftJoin('{{%mall}} m', 'm.mall_id=o.mall_id')
            ->where("r.refund_status>=1");


        if(isset($query['order_no']) && $query['order_no']!=''){
            $order->andWhere(['o.order_no'=>$query['order_no']]);
        }

        if(isset($query['goods_id']) && $query['goods_id']!=0){
            $order->andWhere(['o.goods_id'=>$query['goods_id']]);
        }
        if(isset($query['goods_name']) && $query['goods_name']!=''){
            $order->andWhere(['g.goods_name'=>$query['goods_name']]);
        }
        if(isset($query['category_id']) && $query['category_id']!=0){
            $order->andWhere(['c.category_id'=>$query['category_id']]);
        }

        if(isset($query['order_start_time']) && $query['order_start_time']!=''){
            $order->andWhere(['>=','o.order_time',date("Y-m-d 00:00:00", strtotime($query['order_start_time']))]);
        }
        if(isset($query['order_end_time']) && $query['order_end_time']!=''){
            $order->andWhere(['<=','o.order_time',date("Y-m-d 23:59:59", strtotime($query['order_end_time']))]);
        }

        if(isset($query['pay_start_time']) && $query['pay_start_time']!=''){
            $order->andWhere(['>=','o.pay_time',date("Y-m-d 00:00:00", strtotime($query['pay_start_time']))]);
        }
        if(isset($query['pay_end_time']) && $query['pay_end_time']!=''){
            $order->andWhere(['<=','o.pay_time',date("Y-m-d 23:59:59", strtotime($query['pay_end_time']))]);
        }

        $order->orderBy('order_id desc');
        if($limit)
            $order->offset(($page - 1) * $limit)->limit($limit);
        $lists = $order->asArray()->all();
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $order->count(),
                'page' => $page,
                'limit' => $limit,
                'url' => 'order/refund',
                'query' => $query
            ]
        ];
        return $return;
    }

    public function shippingOrder($order_id,$express_company,$express_no,$shipping_remark=''){
        $order = Order::findOne($order_id);
        if($order->order_status!=1 || $order->shipping_status!=0 || $order->refund_status!=0){
            return false;
        }else{
            $time = date('Y-m-d H:i:s');
            $order->shipping_status = 1;
            $order->shipping_time = $time;
            $order->update_time = $time;
            $order->express_company = $express_company;
            $order->express_no = $express_no;
            if($shipping_remark)
                $order->shipping_remark = $shipping_remark;
            return $order->save();
        }
    }

    public function shippingOrder1($order_no,$express_company,$express_no,$shipping_remark=''){
        $order = Order::findOne(['order_no'=>$order_no]);
        if($order->order_status!=1 || $order->shipping_status!=0 || $order->refund_status!=0){
            return false;
        }else{
            $time = date('Y-m-d H:i:s');
            $order->shipping_status = 1;
            $order->shipping_time = $time;
            $order->update_time = $time;
            $order->express_company = $express_company;
            $order->express_no = $express_no;
            if($shipping_remark)
                $order->shipping_remark = $shipping_remark;
            return $order->save();
        }
    }

    public function refundOrder($refund_id,$refund_amount,$refund_remark=''){
        $refund = Refund::findOne($refund_id);
        if($refund->refund_status==0){
            return false;
        }else{
            $db=\Yii::$app->db;
            $transaction = $db->beginTransaction();
            $time = date('Y-m-d H:i:s');
            $order = Order::findOne($refund->order_id);
            if($refund->refund_status==1){
                $refund->refund_status = 2;
                $order->refund_status = 2;
            }elseif($refund->refund_status==2){
                $refund->refund_status = 4;
                $order->refund_status = 4;
                $refund->over_time=$time;
            }
            $refund->refund_amount=$refund_amount;
            $refund->update_time = $time;
            $order->update_time = $time;
            $refund->admin_id = 1;
            if($refund_remark)
                $refund->refund_remark=$refund_remark;
            $result1 = $refund->save();
            $result2 = $order->save();
            if($result1 && $result2){
                $transaction->commit();
                return true;
            }else{
                $transaction->rollBack();
                return false;
            }
        }
    }

    public function refundOrder1($refund_no,$refund_amount,$refund_remark=''){
        $refund = Refund::findOne(['refund_no'=>$refund_no]);
        if($refund->refund_status==0){
            return false;
        }else{
            $db=\Yii::$app->db;
            $transaction = $db->beginTransaction();
            $time = date('Y-m-d H:i:s');
            $order = Order::findOne($refund->order_id);
            if($refund->refund_status==1){
                $refund->refund_status = 2;
                $order->refund_status = 2;
            }elseif($refund->refund_status==2){
                $refund->refund_status = 4;
                $order->refund_status = 4;
                $refund->over_time=$time;
            }
            $refund->refund_amount=$refund_amount;
            $refund->update_time = $time;
            $order->update_time = $time;
            $refund->admin_id = 1;
            if($refund_remark)
                $refund->refund_remark=$refund_remark;
            $result1 = $refund->save();
            $result2 = $order->save();
            if($result1 && $result2){
                $transaction->commit();
                return true;
            }else{
                $transaction->rollBack();
                return false;
            }
        }
    }

    public function rejectRefund($refund_id){
        $refund = Refund::findOne($refund_id);
        if($refund->refund_status!=1){
            return false;
        }else{
            $db=\Yii::$app->db;
            $transaction = $db->beginTransaction();
            $time = date('Y-m-d H:i:s');
            $order = Order::findOne($refund->order_id);
            $refund->refund_status = 3;
            $order->refund_status = 3;
            $refund->update_time = $time;
            $order->update_time = $time;
            $refund->admin_id = 1;
            $result1 = $refund->save();
            $result2 = $order->save();
            if($result1 && $result2){
                $transaction->commit();
                return true;
            }else{
                $transaction->rollBack();
                return false;
            }
        }
    }


}